<!-- 嵌入公式 -->
<template>
  <j-modal
    title="函数试算"
    :width="600"
    :visible="visible"
    :maskClosable="false" 
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    :confirmLoading="confirmLoading"
  >
    <a-spin :spinning="confirmLoading">
      <j-form-container>
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
               <a-form-model-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="calcDate">
                  <a-date-picker v-model="model.calcDate"  style="width:100%;"/>
              </a-form-model-item>
              <a-form-model-item label="槽号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dataSoltId">
              <a-select show-search placeholder="请选择槽号" v-model="model.dataSoltId">
                <a-select-option v-for="item in slotIdList" :key="item" :value="item">
                  {{ item }}
                </a-select-option>
              </a-select>

                <!-- <j-select-data-slot
                  placeholder="请选择槽号"
                  :multi="false"
                  :buttons="false"
                  v-model="model.dataSoltId"
                ></j-select-data-slot> -->
              </a-form-model-item> 
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
    <a-result status="success" v-if="runResultShow" :title="runResult" sub-title="试算成功"> </a-result>
  </j-modal>
</template>

<script>
import moment from 'moment'
import { getAction, postAction } from '@/api/manage'
import JSelectDataSlot from '@/components/jeecgbiz/JSelectDataSlot'
export default {
  name: 'try',
  components: { JSelectDataSlot },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      title: '',
      width: 1000,
      visible: false,
      disableSubmit: false,
      slotIdList: [],
      model: {
        formulaDetailId: '',
        dataSoltId: '', //选择的槽
        calcDate:'',
      },
      validatorRules: {
        dataSoltId: [{ required: true, message: '请选择槽号!' }],
        calcDate: [{ required: true, message: '请选择日期!' }],
      },
      confirmLoading: false,
      runResultShow: false,
      runResult: '', //试算结果
    }
  },
  props: {
    formulaId: {
      type: Number,
    },
  },
  methods: {
    loadDataSlot() {
      this.slotIdList = []
      getAction('/dataSlot/dataSlot/list', { pageSize: 9999 })
        .then((res) => {
          if (res.success) {
            var dataSource = res.result.records
            for (let i = 0; i < dataSource.length; i++) {
              this.slotIdList.push(dataSource[i].slotId)
            }
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    show(record) {
      this.visible = true
      this.confirmLoading = false
      this.model = Object.assign({}, { dataSoltId: '', formulaDetailId: record.id,
       calcDate: moment(new Date()).format('YYYY-MM-DD'), //计算时间
      })
      this.runResultShow = false
      this.runResult = ''
      this.loadDataSlot();
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      var that = this;
      this.runResultShow = false
      this.runResult = '' 
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.confirmLoading = true
          console.log('try-data:', JSON.stringify(this.model))
          var calcDate = moment(that.model.calcDate).format('YYYY-MM-DD')
          postAction(
            '/userFormula/userFormula/tryFormulaDetail?dataSoltId=' +
              that.model.dataSoltId +
              '&formulaDetailId=' +
              that.model.formulaDetailId+
              '&calcDate='+ calcDate 
          ).then((res) => {
            this.confirmLoading = false
            if (res.success) {
              this.runResultShow = true
              this.runResult = res.result
              // this.$success({
              //   title: '试算结果',
              //   // JSX support
              //   content: (
              //     <div>
              //       <p>{res.result}</p>
              //     </div>
              //   ),
              // })
            } else {
              that.$message.error(res.message)
            }
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style scoped>
</style>